package com.flca.frw.components
{
	import com.flca.frw.interfaces.IModel;
	import com.flca.frw.util.GuiUtil;
	
	import flash.display.DisplayObject;
	
	import mx.collections.ArrayCollection;
	import mx.containers.VBox;
	import mx.controls.Alert;
	import mx.managers.PopUpManager;
	
	public class AssociationManyCtrl extends VBox
	{
		public const ASS_LOOKUP:int 	= 0;
		public const ASS_DETFORM:int = 1;
		public const ASS_MIXED:int 		= 2;
		
		/**
		 * This collection is used to fill the grid
		 */
		[Bindable]
		public var assValues : ArrayCollection;

		/**
		 * Classname of xxxModel to control label and colums
		 */ 
		public var assModel : IModel; 
		
		/**
		 * The detailform to display after add/edit (required)
		 */ 
		public var assDetailForm : DisplayObject; 
		
		[Inspectable]
		[Bindable]
		public var rowcount : int = 4;

		/**
		use one the const above. This correspons with assDetailForm
		With LOOKUP you probably use the LookupForm
		With BELONGS_TO you probably use a XxxDetailForm (or similar)
		With MIXED you probably use a XxxMaintForm (or similar)
		*/
		public var assModus : int;
				
		/**
		this is used to center the popup
		*/
		public var assParentWindow:DisplayObject;
				
		/**
		convenience method to pass all params for modus = LOOKUP
		*/		
		public function setupForLookup(values: ArrayCollection, 
			formatModel :IModel, parentWindow: DisplayObject) : void 
		{
			assModus = ASS_LOOKUP;
			assValues = values;
			assModel = formatModel;
			assParentWindow = parentWindow;
			init();
		}

		/**
		convenience method to pass all params for modus = LOOKUP
		*/		
		public function setupForDetailForm(values: ArrayCollection, 
			detailForm:DisplayObject, formatModel :IModel, parentWindow: DisplayObject) : void 
		{
			assModus = ASS_DETFORM;
			assValues = values;
			assDetailForm = detailForm;
			assModel = formatModel;
			assParentWindow = parentWindow;
			init();
		}
				
			
		private var popupForm : DetailForm;
		private var selectForm : LookupForm;
		private var objectGetter : Function;
		private var selectedIndex : int;
	
		public function init() : void 
		{
			if (assValues != null) {
				view.datagrid.dataProvider = assValues;
			} else {
				view.datagrid.dataProvider = new ArrayCollection();
			}
			
			if (assModel != null && assModel.getGridColumns() != null) {
				view.datagrid.columns = assModel.getGridColumns();			
			}
			
		}

		protected function onAddClicked() : void {
			selectedIndex = -1
			if (assModus == ASS_LOOKUP) {
				onLookupClicked();	
			} else {
				assModel.makeNew();
				onAddOrEditClicked(assModel.getEntity);
			}
		}

		protected function onEditClicked() : void {

			selectedIndex = view.datagrid.selectedIndex;
			if (assModus == ASS_LOOKUP) {
				onLookupClicked();	
			} else {
				onAddOrEditClicked(selectedItemGetter);
			}
		}
		
		private function selectedItemGetter() : Object {
			return view.datagrid.selectedItem;
		}

		private function onAddOrEditClicked(aGetter:Function) : void {
			objectGetter = aGetter;
			popupForm = PopUpManager.createPopUp(parentWnd, DetailForm, true) as DetailForm;
			popupForm.detailView = assDetailForm;
			popupForm.onCancelClicked = onDetailFormCancelClicked;
			popupForm.onSaveClicked = onDetailFormSaveClicked;
			popupForm.setModelGetter(objectGetter);
			popupForm.init();
			popupForm.updateViewFromModel();
			PopUpManager.centerPopUp(popupForm);
			popupForm.y = popupForm.y + 100;//TODO
		}

		private function onLookupClicked() : void {
			selectForm = PopUpManager.createPopUp(parentWnd, LookupForm, true) as LookupForm;
			PopUpManager.centerPopUp(selectForm);
			selectForm.onCancelClicked = onLookupFormCancelClicked;
			selectForm.onSelectClicked = onLookupFormSelectClicked;
			selectForm.gridFormatter = assModel;
			if (assModel.getLookupValues != null) {
				showPopup(assModel.getLookupValues());
			} else {
				Alert.show("assSelectListGetter is null");
			}
		}

		private function showPopup(arrcol:ArrayCollection) : void 
		{
			selectForm.lookupValues = arrcol;
			selectForm.init();
			selectForm.y = selectForm.y + 100;
			PopUpManager.centerPopUp(selectForm);
		}
		
		private function onLookupFormSelectClicked() : void {
			var item:Object = selectForm.idLookupValues.selectedItem;
			var group:Object = assModel.assemble(item);
			GuiUtil.updateDataGrid(view.datagrid, group, selectedIndex);
			PopUpManager.removePopUp(selectForm);
		}
		
		private function onLookupFormCancelClicked() : void {
			PopUpManager.removePopUp(selectForm);
		}
		
		protected function onDelClicked(selcItem:Object) : void {
			selectedIndex = view.datagrid.selectedIndex;
			GuiUtil.removeFromDataGrid(view.datagrid, selectedIndex);
		}
		
		private function onDetailFormSaveClicked() : void {
			if (popupForm.validate()) {
				popupForm.updateModelFromView();
				GuiUtil.updateDataGrid(view.datagrid, objectGetter(), selectedIndex);
				handleGuiState();
				PopUpManager.removePopUp(popupForm);
			}
		}
		
		private function onDetailFormCancelClicked() : void {
			PopUpManager.removePopUp(popupForm);
		}
		
		protected function onDataGridChanged() : void {
			handleGuiState();						
		}
		
		internal function get view() : AssociationManyForm {
			return this as AssociationManyForm;
		}

		internal function handleGuiState():void {
			view.btnAdd.enabled = true;
			view.btnEdit.enabled = view.datagrid.selectedIndex >= 0;
			view.btnDel.enabled = view.datagrid.selectedIndex >= 0;
		}
		
		protected function trl(s:String) : String {
			return s;	
		}

		protected function  get parentWnd() : DisplayObject {
			if (assParentWindow != null) {
				return assParentWindow;
			} else {
				return this;//TODO
			}
		}
	}
}